package org.example.java.regExp;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 宽断言
 * (?=exp)	匹配exp前面的位置
 * (?<=exp)	匹配exp后面的位置
 * (?!exp)	匹配后面跟的不是exp的位置
 * (?<!exp)	匹配前面不是exp的位置
 * Created by Juntao.Zhang on 2014/5/22.
 */
public class Test {
  public static List<String> getTeacherList(String managers) {
    List<String> ls = new ArrayList<>();
    Pattern pattern = Pattern.compile("(?<=\\[)(.+?)(?=\\])");
    Matcher matcher = pattern.matcher(managers);
    while (matcher.find())
      ls.add(matcher.group());
    return ls;
  }

  public static void getInfo(String str, String reg) {
    List<String> ls = new ArrayList<>();
//    Pattern pattern = Pattern.compile("^(\\d{1,3}\\.){3}(\\d{1,3})");
//    Pattern pattern = Pattern.compile("^(\\s*-\\s*(\\w*)(\\s*))");
//    Pattern pattern = Pattern.compile("(?<=\\[)(.+?)(?<=\\])");
//    Pattern pattern = Pattern.compile("(?=\")(.*?)(?<=\")");
//    Pattern pattern = Pattern.compile("((\")(.*?)(\"))");
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = pattern.matcher(str);
    if (matcher.find()) {
      System.out.println(str);
      System.out.println(matcher.group());
      str = str.substring(matcher.end());
      System.out.println(str);
    }
  }

  public static void main(String[] args) {
//    System.out.println(getTeacherList("张峻滔01 [123] 张峻滔021 [43645] "));

    String str = "10.1.1.95  - e800 [18/Mar/2005:12:21:42 +0800]  \"GET /stats/awstats.pl?config=e800 HTTP/1.1\" 200 899 \"http://10.1.1.1/pv/\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Maxthon)\"";
//    System.out.println(getInfo(" \"sdfsd\" "));
//    getInfo(str);
    getInfo("sd[sdf]sd", "(?<=\\[)(.+?)(?<=\\])");
    System.out.println();
    getInfo("sd[sdf]sd", "(?=\\[)(.+?)(?=\\])");


  }

}
